#!/bin/bash

# sonoffmorse.sh converts the string that is given as arguments to morse and
# uses the result to make a sonoff basic blink the message. The sonoff is a
# sonoff basic flashed with sonoff-tasmota.  The messages are sent via a mqtt
# broker.  Needs morsegen(1).

# Usage:
#        ./sonoffmorse.sh please bring me pizza

# Edit these to your likings:
topic='sonoff1'
mqtt_broker='192.168.1.1'

pause () {
   sleep 2
}
init () {
   mosquitto_pub -h $mqtt_broker -t cmnd/$topic/power -m 0
   mosquitto_pub -h $mqtt_broker -t cmnd/$topic/blinkcount -m 1
}
short () {
   mosquitto_pub -h $mqtt_broker -t cmnd/$topic/blinktime -m 5
   mosquitto_pub -h $mqtt_broker -t cmnd/$topic/power -m 3
}
long () {
   mosquitto_pub -h $mqtt_broker -t cmnd/$topic/blinktime -m 10
   mosquitto_pub -h $mqtt_broker -t cmnd/$topic/power -m 3
}

instring="$*"
morse=$(echo -n $instring | morsegen - )

init ; pause
for (( i=0 ; i<${#morse} ; i++ )) ; do
   beep="${morse:$i:1}"
   echo -n "$beep"
   if [ "$beep" == "." ] ; then short ; fi
   if [ "$beep" == "-" ] ; then long ; fi
   if [ "$beep" == " " ] ; then pause ; fi
   pause
done
echo

# vim:tw=0
